#!/bin/bash

# block read testing
# give it a device and it will go after it


if [ -z "$3" ] ; then
    echo "Usage: $0 device count percent"
    echo "	device : device to test(eg. /dev/sda)"
    echo "	count : number of blocks to test for each chunk"
    echo "	percent : percentage of disk surface coverage"
    exit 2
fi

renice +5 -p $$
bsize=1024
count=$2
percent=$3

disksize=`fdisk -l $1 | grep -m 1 $1 | awk '{print $5}'`
let disksize="$[disksize/bsize]"
echo "disksize : $disksize"
let chunks=$[disksize/count]
echo "chunks : $chunks"
let blocks=$(echo "($chunks * $percent / 100)" | bc)
echo "blocks : $blocks"
let interleave=$(echo "($chunks / $blocks)" | bc)
echo "interleave : $interleave"

echo "
Sequential Disk Test
Disk coverage = $percent%, $((count*blocks*bsize/1024))KB 
Chunk Size = $((chunks/1024))KB
blocks = $blocks
interleave = $interleave"

for ((i=0; i <= blocks/2 ; i+=1))
do
  let "start=count*i*interleave"
  let "end=start+count"
  let "start2=disksize-start"
  let "end2=disksize-end"
  echo "Testing at location $((100*end/disksize))% - $start to $end - $i of $((blocks/2))  "
  badblocks -b $bsize $1 $end $start 2&>1
  echo "Testing at location $((100*end2/disksize))% - $start2 to $end2 - $((i+blocks/2)) of $blocks  "
  badblocks -b $bsize $1 $end2 $start2 2&>1
done
exit $?

########################################################################33
# block read testing
# give it a device and it will go after it

# start from both ends
# moving heads back and fort

if [ -z "$3" ] ; then
    echo device count skip_blocks required
    exit 2
fi

bsize=1024
count=$2
blocks=$3

disksize=`fdisk -l $1 | grep $1 | awk '{print $5}'`
let disksize="$[disksize/bsize]";

echo "Testing disk, seeking back and forth"
for ((i=0; i <= (disksize/2) ; ))
do
  let "end=i+(bsize*count)"
  let "j=disksize-i"
  let "chunk2_begin=j-(bsize*count)"
 
  #./bin/flushb $1      #flush cash
  echo "Testing $i to $end - $((end-i))"
  nice -n 5 badblocks -c $count -b $bsize $1 $end $i  
  echo "Testing $chunk2_begin to $j - $((j-chunk2_begin))"
  nice -n 5 badblocks -c $count -b $bsize $1 $j $chunk2_begin 
  #nice -n 5 badblocks -c $count -b $bsize $1 $end2 $start2 
  let "i=end+(bsize*blocks)"
  #echo "Skipping $((bsize*blocks)) blocks"
  #./bin/flushb $1
done

exit $?
